using System;
using System.Collections.Generic;
using System.Linq;
using AutoStopData.Model;

namespace AutoStopData.Domains
{
    public partial class UserTypes
    {
        private static readonly List<EnumPair> Data;

        static UserTypes()
        {
            Data = new List<EnumPair> 
            {
                new EnumPair(Name.Administrator, CodeKey.ADM),
                new EnumPair(Name.Driver, CodeKey.DRI),
                new EnumPair(Name.User, CodeKey.USR)
            };
        }

        private enum CodeKey
        {
            ADM,
            DRI,
            USR
        }

        public enum Name
        {
            None,
            Administrator,
            Driver,
            User
        }

        public static string GetCode(Name name)
        {
            string retVal;
            var enumPair = Data.FirstOrDefault(o => o.Name == name);
            if (enumPair != null)
            {
                retVal = Enum.GetName(typeof(CodeKey), enumPair.CodeKey);
            }
            else
            {
                throw new Exception("Unknown domain: " + name);
            }
            return retVal;
        }

        public static Name GetName(string code)
        {
            Name retVal;
            CodeKey codeKey = (CodeKey)Enum.Parse(typeof(CodeKey), code);
            var enumPair = Data.FirstOrDefault(o => o.CodeKey == codeKey);
            if (enumPair != null)
            {
                retVal = enumPair.Name;
            }
            else
            {
                throw new Exception("Unknown domain: " + code);
            }
            return retVal;
        }

        private class EnumPair
        {
            public Name Name { get; private set; }

            public CodeKey CodeKey { get; private set; }

            public EnumPair(Name name, CodeKey codeKey)
            {
                Name = name;
                CodeKey = codeKey;
            }
        }
    }
}